<?php

namespace Admin\Controller;
/**
 * 客户管理
 * @author Dinghua
 *
 */
class CustomerController extends AdminController{
	
	/**
	 * 客户管理[管理员使用]
	 */
	public function index() {
		if ( $this->_userInfo['group_id'] == '5') {
			$this->redirect('Customer/mycustomer');
		}
		$map = array();
		
		if(isset($_GET['status'])){
			$map['status'] = I('status');
			$status = $map['status'];
		}
		
		$key = I('get.key');
		if ($key) {
			$where['name'] = array('like',"%$key%"); //姓名
			$where['phone'] = array('like',"%$key%"); //电话
			$where['phone1'] = array('like',"%$key%"); //电话
			$where['_logic'] = 'OR';
			$map['_complex'] = $where;
		}
		
		$list = $this->lists('Customer', $map, 'id DESC');

		$this->assign('_list',$list);
		
		// 记录当前列表页的cookie
		Cookie('__forward__',$_SERVER['REQUEST_URI']);
		$this->assign('status', $status);
		$this->meta_title = '客户列表';
		$this->display();
	}
	
	
	/**
	 * 我的客户[业务员使用]
	 */
	public function mycustomer() {
		$map = array();
		
		if(isset($_GET['status'])){
			$map['status'] = I('status');
			$status = $map['status'];
		}
		$map['uid'] = UID; //我的客户
		$key = I('get.key');
		if ($key) {
			$where['name'] = array('like',"%$key%"); //姓名
			$where['phone'] = array('like',"%$key%"); //电话
			$where['phone1'] = array('like',"%$key%"); //电话
			$where['_logic'] = 'OR';
			$map['_complex'] = $where;
		}
		
		$list = $this->lists('Customer', $map, 'id DESC');
		
		$this->assign('_list',$list);
		
		// 记录当前列表页的cookie
		Cookie('__forward__',$_SERVER['REQUEST_URI']);
		$this->assign('status', $status);
		$this->meta_title = '我的客户';
		$this->display();
	}
	
	
	/**
	 * 新增
	 */
	public function add() {
		if ($this->_userInfo['group_id'] != '1' and !IS_ROOT) {
			$this->assign('cur', true);
		}
		$this->assign('customerStatus', customerStatus('', 'all'));
		$this->meta_title = '新增客户';
		$this->display();
	}
	

	
	/**
	 * 编辑客户
	 * @param string $id 商品ID
	 */
	public function edit($id = NULL) {
		if (empty($id)) $this->error('参数错误');
		$info = D('Customer')->info($id);
		if (($this->_userInfo['group_id'] != '1') and !IS_ROOT) {
			if ($info['uid'] != UID) {
				$this->error('参数错误');
			} else {
				if ($info['status'] == 4) {
					$this->assign('cur', true);
				}
			
			}
		}
		
		$this->assign('info', $info);
		$this->assign('customerStatus', customerStatus('', 'all'));
		$this->meta_title = '编辑客户信息';
		
		$this->display('add');
	}
	
	
	/**
	 * 更新或新增一条数据
	 */
	public function update(){
		
		$id = I('id');
		//为空的时候向添加默认的用户
		if (empty($id)) {
			$_POST['uid'] = UID;
			$_POST['adduid'] = UID;
			$_POST['status'] = 4; //待审核
		}
		$Customer = D('Customer');
		$res = $Customer->update();
		if(!$res){
			$this->error($Customer->getError());
		}else{
			$this->success($res['id']?'更新成功':'新增成功', Cookie('__forward__'));
		}
	}
	
	/**
	 * 分配
	 */
	public function allocation($id = NULL) {
		if (IS_POST) {
			
			$Customer = M('Customer');
			$map['id'] = I('post.id');
			$data['uid'] = I('post.uid');
			$data['status'] = 2;
			$res = $Customer->where($map)->save($data);
			if(!$res){
				$this->error($Customer->getError());
			}else{
				$this->success('操作成功！', Cookie('__forward__'));
			}
			
		} else {
			//读取所有业务组
			$map['group_id'] = 5;
			$uid = M('auth_group_access')->where($map)->getField('uid', true);
			$where['uid'] = array('in', $uid);
			$list = M('Member')->where($where)->field('uid,nickname')->select();
			$this->assign('_list', $list);
			$this->assign('id', $id);
			$this->meta_title = '分配客户';
			$this->display();
		}
		
	}
	
}